Creating Command Bar Submenu

Juanderful1

New Member
Joined
Dec 26, 2002
Messages
12
I am trying to add a new control to the File Pull Down menu and not having much luck.

I am using the code below, it seems simple. But when I try to run it I get this error message :
Error : Object Variable or With Block Variable not set


Public Sub NewThing()
Dim x
Set x = CommandBars("Worksheet Menu Bar").Controls("File").Controls.Add(Type:=msoControlPopup)
End Sub

Anyone know what I am doing wrong??
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi, you will need to define x as a CommandBarControl control

eg


Code:
Public Sub NewThing()
Dim x As CommandBarControl
Set x = CommandBars("Worksheet Menu Bar").Controls("File").Controls.Add(Type:=msoControlPopup)
End Sub

Note that as is it will add a menu item with no name or anything. Have you got more code or is this all you have so far?
 
Upvote 0
Thanks for the advice, but I added the Dim x As CommandBarControl and I am still getting the same error...Help!!!!!!!!!!!!!!
 
Upvote 0
It's not clear from your limited information exactly what you are trying to do. If your goal is to add a menu item to the File menu on the command bar, and if you want to attach a macro to that item, and if you only want that item to be visible on a certain workbook, then the following code shows one way to accomplish that.

There are a lot of ifs in the above paragraph because we are not mind readers, only programmers. This works when tested, assuming the above ifs are guessed accurately. If not, modify to suit or post back.

Example - -

In your workbook module:

Option Explicit

Private Sub Workbook_Activate()
Dim CB As CommandBar
Dim CBC As CommandBarControl
Dim CBCC As CommandBarButton
Set CB = Application.CommandBars.ActiveMenuBar
Set CBC = CB.Controls("File")
Set CBCC = CBC.Controls.Add
With CBCC
.Caption = "Test"
.OnAction = "TestMacro"
.FaceId = 36
End With
End Sub

Private Sub Workbook_Deactivate()
On Error Resume Next
Application.CommandBars.ActiveMenuBar.Controls("File").Controls("Test").Delete
End Sub



In a standard module (modify for your actual macro):

Sub TestMacro()
MsgBox "This is a test of the File custom menu item."
End Sub
 
Upvote 0
Thanks for your help.

My goal is very limited here. I just want to understand why the code I have does not work ( produces an error message ).

I think I can do the rest ( like attaching a macro ) once I understand what I'm doing wrong.

Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,101
Messages
6,123,092
Members
449,095
Latest member
gwguy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top